﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using System.Xml;

namespace KingOfCSharp.iHunter.Plugins.BBS
{
    public partial class BbsNewItemEditor : UserControl , KingOfCSharp.iHunter.UI.IFieldTextBoxOwner
    {
        private Control lastControlFocused = null;
        private bool needNotifyMsgInvalidReason = false;

        public BbsNewItemEditor()
        {
            InitializeComponent();

            Init();
        }

        private bool isMail;
        private string errorReason;
        private bool modified;

       

        private void Init()
        {
            
            textBox_Contents.SetOwner(this, "Press ESC to insert control char *");
            textBox_Receiver.SetOwner(this, "Board");
            textBox_Title.SetOwner(this, "Title");

            textBox_Contents.EnableTip = true;

            comboBox_Type.SelectedIndex = 0;

            modified = false;
        }

        #region IMessageEditor Members

        public IMessage GetMessage()
        {
            needNotifyMsgInvalidReason = true;
            bool msgValid = MessageValid;
            needNotifyMsgInvalidReason = false;
            if (msgValid)
            {
                IMessage msg = new Message();

                if (isMail)
                {
                    msg.Receiver = textBox_Receiver.Text;
                    msg.Category = Mail.MailCategory;

                    msg.CustomFields["MailType"] = "NormailMail";
                }
                else
                {
                    msg.Category = textBox_Receiver.Text;
                }

                msg.Title = textBox_Title.Text;
                msg.Content = textBox_Contents.Text.Replace(Character.ESC+"", Character.DoubleESC);

                return msg;
            }
            else
            {
                //MessageBox.Show(errorReason);
                return null;
            }
        }

        public IMessage GetDraft()
        {
            IMessage msg = new Message();

            if (isMail)
            {
                msg.Receiver = textBox_Receiver.Text;
                msg.Category = Mail.MailCategory;

                msg.CustomFields["MailType"] = "NormailMail";
            }
            else
            {
                msg.Category = textBox_Receiver.Text;
            }

            msg.Title = textBox_Title.Text;
            msg.Content = textBox_Contents.Text;

            return msg;
        }

        public bool ImportMessage(IMessage source)
        {
            if (Mail.MailCategory.Equals(source.Category))
            {
                comboBox_Type.SelectedIndex = 1;
                textBox_Receiver.Text = source.Receiver;
            }
            else
            {
                textBox_Receiver.Text = source.Category;
                comboBox_Type.SelectedIndex = 0;
            }

            textBox_Title.Text = source.Title;
            textBox_Contents.Text = source.Content;

            modified = false;
            return MessageValid;
        }

        public bool MessageModified
        {
            get 
            {
                return modified;
            }
            set
            {
                modified = value;
            }

        }

        public bool MessageValid
        {
            get
            {
                if (isMail)
                {
                    if (textBox_Receiver.Text == null || textBox_Receiver.Text.Trim() == "")
                    {
                        errorReason = "Receiver can not be empty";
                        ShowMessageInvalidReason(textBox_Receiver, errorReason);
                        return false;
                    }

                    if (textBox_Receiver.Text.Length > 20)
                    {
                        errorReason = "Receiver should be a string whose length no larger than 20";
                        ShowMessageInvalidReason(textBox_Receiver, errorReason);
                        return false;
                    }
                }
                else
                {
                    if (textBox_Receiver.Text == null || textBox_Receiver.Text.Trim() == "")
                    {
                        errorReason = "Board can not be empty";
                        ShowMessageInvalidReason(textBox_Receiver, errorReason);
                        return false;
                    }

                    if (textBox_Receiver.Text.Length > 40)
                    {
                        errorReason = "Board name should be a string whose length no larger than 40";
                        ShowMessageInvalidReason(textBox_Receiver, errorReason);
                        return false;
                    }
                }

                if (textBox_Title.Text == null || textBox_Title.Text.Trim() == "")
                {
                    errorReason = "Title can not be empty";
                    ShowMessageInvalidReason(textBox_Title, errorReason);
                    return false;
                }

                if (Character.GetStringLengthInTerm(textBox_Title.Text) >= 80)
                {
                    errorReason = "Title length should be less than 80 (Chinese character counts as two chars)";
                    ShowMessageInvalidReason(textBox_Title, errorReason);
                    return false;
                }

                return true;
            }
        }

        public System.Xml.XPath.XPathNavigator ContextualCommands
        {
            get
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(Properties.Resources.BbsEditorUI);

                return doc.CreateNavigator();
            }
        }


        #endregion

        private void comboBox_Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_Type.SelectedIndex == 0)
            {
//                panel_MailReceiver.Size = new Size(panel_MailReceiver.Size.Width, 0);
                label_Receiver.Text = "Board:";
                textBox_Receiver.SetTipContent("Board");
                isMail = false;
            }
            else
            {
  //              panel_MailReceiver.Size = new Size(panel_MailReceiver.Size.Width, 30);
                label_Receiver.Text = "Receiver:";
                textBox_Receiver.SetTipContent("Mail Receiver");
                isMail = true;
            }

            modified = true;
        }

        
        private void textBox_Contents_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                textBox_Contents.Text += Character.ESC;
            }
        }

		#region IMessageEditor Members


		public void ExcuteCommand(CommandUIEventArgs uiElement)
		{
            if (lastControlFocused is TextBox)
            {
                TextBox txtBox = lastControlFocused as TextBox;

                switch (uiElement.Name)
                {
                    case "Edit.Cut":
                        txtBox.Cut();
                        break;

                    case "Edit.Copy":
                        txtBox.Copy();
                        break;

                    case "Edit.Paste":
                        txtBox.Paste();
                        break;

                    case "Edit.Undo":
                        txtBox.Undo();
                        break;

                    default:
                        return;
                }

                EnableClipboardButtons(txtBox.SelectionLength > 0, txtBox.CanUndo, false);
            }
		}

		public event CommandUIEventHandler UpdateCommandUI;

		#endregion

        #region IFieldTextBoxOwner Members

        public void EnableClipboardButtons(bool cut, bool undo, bool redo)
        {
            if (this.UpdateCommandUI != null)
            {
                CommandUIEventArgs updateArgs = new CommandUIEventArgs();
                updateArgs.Name = "Edit.Cut";
                updateArgs.Enabled = cut;
                updateArgs.Mask = CommandUIMask.None | CommandUIMask.Enabled;

                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Copy";
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Paste";
                updateArgs.Enabled = Clipboard.ContainsText();
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Undo";
                updateArgs.Enabled = undo;
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Redo";
                updateArgs.Enabled = redo;
                this.UpdateCommandUI(this, updateArgs);
            }
        }

        public void DisableClipboardButtons()
        {
            if (this.UpdateCommandUI != null)
            {
                CommandUIEventArgs updateArgs = new CommandUIEventArgs();
                updateArgs.Name = "Edit.Cut";
                updateArgs.Enabled = false;
                updateArgs.Mask = CommandUIMask.None | CommandUIMask.Enabled;

                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Copy";
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Paste";
                updateArgs.Enabled = false;
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Undo";
                updateArgs.Enabled = false;
                this.UpdateCommandUI(this, updateArgs);

                updateArgs.Name = "Edit.Redo";
                updateArgs.Enabled = false;
                this.UpdateCommandUI(this, updateArgs);
            }
        }

        public void EnterFieldTextBox(KingOfCSharp.iHunter.UI.FieldTextBox txtBox)
        {
            lastControlFocused = txtBox;
        }

        public void MarkMessageModified()
        {
            modified = true;
        }

        #endregion

        private void ShowMessageInvalidReason(KingOfCSharp.iHunter.UI.FieldTextBox txtBox, string reason)
        {
            if (needNotifyMsgInvalidReason)
            {
                txtBox.ShowTip(reason);
            }
        }

        private void comboBox_Type_Enter(object sender, EventArgs e)
        {
            lastControlFocused = comboBox_Type;

            DisableClipboardButtons();
        }
    }
}
